package rickyGame.server
{
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import ricky.Interface.IDispose;
	import ricky.utils.FrameManager;
	import ricky.Interface.IAct;
	import ricky.utils.TimeServer;
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.utils.Utils;
	
	import rickyGame.game.Building;
	import rickyGame.game.Fighter;
	import rickyGame.game.Player;
	import rickyGame.game.action.ActUtils;
	import rickyGame.game.action.ActionHelper;
	import rickyGame.game.action.AnimaAct;
	import rickyGame.game.action.CallPetAct;
	import rickyGame.game.action.CreateArrow;
	import rickyGame.game.action.JumpAct;
	import rickyGame.game.action.ShootAct;
	import rickyGame.game.action.SpellAct;
	import rickyGame.game.skill.SkillData;
	import rickyGame.game.skill.SkillEffectTargetType;
	import rickyGame.net.MsgType;
	import rickyGame.net.WorkQueue;
	import rickyGame.server.effect.EffectHelper;
	import rickyGame.server.state.InteravalAct;
	import rickyGame.server.state.SpellInteravalAct;

	/**
	 * 技能战斗控制
	 */
	public class SpellController implements IDispose
	{
		public function SpellController(oprator:Fighter)
		{
			_oprator=oprator;
			_serverWorld=ServerWorld.inst;
		}
		
		
		/**
		 * 释放技能入口
		 */
		public function spell(target:Fighter,point:Point,abiliData:SkillData):void
		{
			if(checkIf(abiliData))
			{
				this._abiliData=abiliData;
				var relese:String=abiliData.release;
				this[relese](target,point);
			}
		}
		
		//*******************************************
		//魔法流程
		//******************************************
		/**
		 * 使用物品
		 */
		public function useGoods(target:Fighter,point:Point):void
		{
			if(target is Building && target.group!=this._oprator.group)
			{
				physical(target,null);
				
			}else	if(target.skills.passive.length>0)
			{
				_abiliData=target.skills.passive[0];
				
				var act:Act2D=null;
				
				if(_abiliData)
					act=installState(this._oprator,_abiliData,[target]);

//				if(act)
//					WorkQueue.instance.pushRecive(MsgType.CLIENT_PLAY_ACTION,null,act);
			}
		}
		
//		private function army(target:Fighter,point:Point):void
//		{
//			var mc:AnimaAct=new AnimaAct(_oprator,this._abiliData.actAnim);
//			var eff:Act;
//			
//			arrowAct=new CreateArrow(_oprator,_abiliData,type,num,target,point);
//			mainAct.addEffectNext(arrowAct);
//			
////			if(_abiliData.model)
////			{
////				eff=new SpellAct(_oprator,_abiliData,target,point);
////				mc.addEffectNext(eff);
////			}
//			
//			//计算效果
//			var effAct:Act=this.calcEffect(_oprator,point,target);
//			mc.addEffectNext(effAct);
//			
//			if(this._abiliData.continueTime>0) //如果持续时间大于0
//			{
//				var sinterAct:SpellInteravalAct=new SpellInteravalAct(_oprator,_abiliData,target,point);
//				ActionHelper.inst.addAct(sinterAct);//开始状态
//			}
//			
//			this.sendActToClient(mc);
//		}
		
		/**
		 * 物理攻击
		 */
		private function physical(target:Fighter,point:Point):void
		{
			var mc:AnimaAct=new AnimaAct(this._oprator,this._abiliData.actAnim);
			var eff:Act2D;
			var act:Act2D;

			
			if(target)
			{
				act=installState(_oprator,_abiliData,[target]);
				mc.addEffectNext(act);
			}
			
			_abiliData.lastUseTime=new Date().getTime();
			var _point:Object;
			if(target)
				_point=target;
			else
				_point=point;
			_oprator.direction=Utils.calcDirection(_oprator,_point);
			
//			WorkQueue.instance.pushRecive(MsgType.CLIENT_PLAY_ACTION,_oprator,mc);

		}
		
		/**
		 * 魔法攻击
		 */
		private function magic(target:Fighter,point:Point):void
		{
			var mc:AnimaAct=new AnimaAct(_oprator,this._abiliData.actAnim);
			var eff:Act2D;
			
			if(_abiliData.model)
			{
				eff=new SpellAct(_oprator,_abiliData,target,point);
				mc.addEffectNext(eff);
			}
			
			//计算效果
			var effAct:Act2D=this.calcEffect(_oprator,point,target);
			mc.addEffectNext(effAct);
			
			if(this._abiliData.continueTime>0) //如果持续时间大于0
			{
				var sinterAct:SpellInteravalAct=new SpellInteravalAct(_oprator,_abiliData,target,point);
				ActionHelper.inst.addAct(sinterAct);//开始状态
			}
			
			this.sendActToClient(mc);
		}
		
		/**
		 * 召唤宠物
		 */
		private function call(target:Fighter,point:Point):void
		{
			var mc:AnimaAct=new AnimaAct(this._oprator,_abiliData.actAnim);
			var eff:Act2D;
			var act:Act2D;
			
			act=new CallPetAct(_oprator,_abiliData);
			mc.addNext(act);
			
			var effAct:Act2D=this.calcEffect(_oprator,point,target);
			mc.addEffectNext(effAct);
			
			this.sendActToClient(mc);
		}
		
		/**
		 * 跳跃
		 */
		private function jump(target:Fighter,point:Point):void
		{
			var mc:AnimaAct=new AnimaAct(_oprator,_abiliData.actAnim);
			var eff:Act2D;
			var act:Act2D;
			
			act=new JumpAct(_oprator,point,_abiliData);
			mc.addNext(act);
			
			var effAct:Act2D=this.calcEffect(_oprator,point,target);
			mc.addEffectNext(effAct);
			
			this.sendActToClient(mc);
		}
		
		
		/**
		 * 箭矢攻击
		 */
		private function arrow(target:Fighter,point:Point):void
		{
			var mainAct:AnimaAct=new AnimaAct(this._oprator,this._abiliData.actAnim);
			var act:Act2D;
			var arrowAct:CreateArrow;
			
			if(_abiliData.model)
			{
				
				var type:String="line";//默认直线
				var num:Number=1;//放箭数量
				var pars:Array=_abiliData.parameter.split(",");
				type=pars[0];
				if(pars.length>1)
					num=Number(pars[1]);
				
				arrowAct=new CreateArrow(_oprator,_abiliData,type,num,target,point);
				mainAct.addEffectNext(arrowAct);
			}
			
			var effAct:Act2D=this.calcEffect(_oprator,point,target);
			mainAct.addEffectNext(effAct);
			
			if(this._abiliData.continueTime>0 && _abiliData.model) //如果持续时间大于0
			{
				arrowAct=new CreateArrow(_oprator,_abiliData,type,num,target,point);
				arrowAct.lockedupComplete=true;
				
				var inter:InteravalAct=new InteravalAct(arrowAct,_abiliData.continueTime,_abiliData.effectInterval);
				ActionHelper.inst.addAct(inter);//开始状态
			}
			
			this.sendActToClient(mainAct);

		}
		
		//****************************************************************
		// 公共方法
		//***************************************************************
		/**
		 * 寻找技能影响的目标
		 * x:Number 释放目标x坐标
		 * y:Number 释放目标y坐标
		 * skill:SkillData 技能数据
		 */
		public static function findEffectTargets(oprator:Actor,x:Number,y:Number,skill:SkillData):Array
		{
			return ActUtils.searchAround(oprator,skill,x,y);
		}
		
		/**
		 * 给多个目标安装魔法状态
		 */
		public static function installState(oprator:Actor,abiliData:SkillData,targets:Array):Act2D
		{
			var arg:Array;
			var fun:Function;
			var act:Act2D;
			var f:String;
			
			var analyzeEffFun:Function=EffectHelper.analyzeEffect;
			
			var fistAct:Act2D;
			var effects:Array=abiliData.effect.split(";");
			
			for each(var a:Fighter in targets)
			{
				if(!a || a.isDead)
					continue;

				for each(f in effects)
				{
					arg=analyzeEffFun(f);
					fun=EffectHelper[arg[0]];
					if(fun!=null)
					{
						act=fun.call(null,oprator,a,arg[1]);
						if(act)
						{
							if(fistAct)
								fistAct.addEffectNext(act);
							else
								fistAct=act;
						}
					}
				}
			}
			
			return fistAct;
		}

		/**
		 * 发送到客户端
		 */
		public function sendActToClient(act:Act2D):void
		{
//			WorkQueue.instance.pushRecive(MsgType.CLIENT_PLAY_ACTION,_oprator,act);
		}
		
		/**
		 * 把行动状态发送到客户端
		 */
		public static function sendInstallStateToClient(act:Act2D):void
		{
//			if(act)
//				WorkQueue.instance.pushRecive(MsgType.CLIENT_INSTALL_STATE,act);
		}
		
		/**
		 * 计算效果影响 和方向
		 */
		public function calcEffect(oprator:Fighter,point:Point,target:Fighter):Act2D
		{
			var targets:Array=[];
			try{
			if(_abiliData.effectRange>0)
				targets=findEffectTargets(oprator,point.x,point.y,this._abiliData);
			else
				targets=[target];
			var act:Act2D=null;
			if(targets)
			{
				act=installState(oprator,_abiliData,targets);
			}
			
			_abiliData.lastUseTime=TimeServer.now();
			oprator.direction=Utils.calcDirection(oprator,point);
			return act;
			}catch(e:*)
			{
				trace(e);
			}
			return null;
		}
		
		/**
		 * 判断是否可以释放技能
		 */
		protected function checkIf(abiliData:SkillData):Boolean
		{
			var cdnow:Number=new Date().getTime();
			var l:Number=abiliData.lastUseTime;
			var dt:Number=(cdnow-l);
			var v:Number=100;//_oprator.fightPro.mp.value
			
			if(abiliData.cast>v) //魔法值不够
			{
				return false;
			}
			if(abiliData.cooldown>dt)	//判断cd时间
			{
				return false;
			}

			return true;
		}

		/**
		 * 销毁对像 释放系统资源
		 */
		public function dispose():void
		{
			this._oprator.fightCon=null;
		}
		//************************ private *************************

		//**************************************************
		//
		//**************************************************
	
		
		//private
		private var _abiliData:SkillData;
		private var _oprator:Fighter;
		private var _serverWorld:ServerWorld;
		//
	}
}